package com.military.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.military.mapper.MilitaryStandardMapper;
import com.military.pojo.MilitaryQueryParam;
import com.military.pojo.MilitaryStandard;
import com.military.pojo.PageResult;
import com.military.service.MilitaryStandardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class MilitaryStandardServiceImpl implements MilitaryStandardService {

    @Autowired
    private MilitaryStandardMapper militaryStandardMapper;

//    /**
//     * 实现类,调用接口查询军标所有信息返回
//     * @return
//     */
//    @Override
//    public List<MilitaryStandard> findAll() {
//        return militaryStandardMapper.findAll();
//    }

    /**
     * 实现类,调用接口分页查询军标信息返回
     * @param page 页码
     * @param pageSize 每页记录数
     * @return
     */
    @Override
    public PageResult<MilitaryStandard> page(Integer page, Integer pageSize) {

//        //1.调用mapper接口查询总记录数
//        Long count = militaryStandardMapper.count();
//
//        //2.调用mapper接口查询分页结果
//        List<MilitaryStandard> rows = militaryStandardMapper.list((page - 1) * pageSize, pageSize);
//
//        //3.封装分页结果PageResult
//        return new PageResult<>(count, rows);

        //1.设置分页参数
        PageHelper.startPage(page, pageSize);

        //2.调用mapper接口执行查询
        List<MilitaryStandard> militaryStandardList = militaryStandardMapper.list();

        //3.解析查询结果并封装
         //将list集合转换成page对象
        Page<MilitaryStandard> p = (Page<MilitaryStandard>) militaryStandardList;
        //p.getTotal(): 总记录数
        return new PageResult<>(p.getTotal(),p.getResult());//获取结果列表
    }

    /**
     * 条件分页查询
     * @return
     */
    @Override
    public PageResult<MilitaryStandard> pageId(MilitaryQueryParam queryParam) {
        //1.设置分页参数
        PageHelper.startPage(queryParam.getPage(), queryParam.getPageSize());

        //2.调用mapper接口执行查询
        List<MilitaryStandard> militaryStandardList = militaryStandardMapper.pageId(queryParam);

        //3.封装分页结果PageResult
         //将集合强转成page对象
        Page<MilitaryStandard> p = (Page<MilitaryStandard>) militaryStandardList;
        //p.getTotal(): 总记录数
        return new PageResult<>(p.getTotal(),p.getResult());
    }

    /**
     * 实现类,调用接口删除指定id的军标信息
     * @param id
     */
    @Override
    public void deleteId(Integer id) {
        militaryStandardMapper.deleteId(id);
    }

    /**
     * 实现类,调用接口添加军标信息
     * @param militaryStandard
     */
    @Override
    public void add(MilitaryStandard militaryStandard) {
        //1. 补全基础属性
//        militaryStandard.setFileType(militaryStandard.getFileType());
//        militaryStandard.setDescription(militaryStandard.getDescription());
//        militaryStandard.setTag(militaryStandard.getTag());
        militaryStandard.setUploadTime(LocalDateTime.now());

        //2.调用接口添加军标信息
        militaryStandardMapper.add(militaryStandard);
    }

    /**
     * 实现类,调用接口查询指定id的军标信息
     * @param id
     * @return
     */
    @Override
    public MilitaryStandard getInfo(Integer id) {
        //没有逻辑直接调用mapper接口根据id查询
        return militaryStandardMapper.getInfo(id);
    }

    /**
     * 实现类,调用接口更新军标信息
     * @param militaryStandard
     */
    @Override
    public void update(MilitaryStandard militaryStandard) {
        //1.补全基础属性
        militaryStandard.setUploadTime(LocalDateTime.now());

        //2.调用接口更新军标信息
        militaryStandardMapper.update(militaryStandard);
    }
}
